package top.zhangjianyong.tools.timer;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.annotation.DoTimer;
import top.zhangjianyong.tools.entity.marirui.JJGZ;
import top.zhangjianyong.tools.service.api.MairuiService;
import top.zhangjianyong.tools.utils.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author ： zhangjianyong
 * @date ： 2022/10/17 9:10 AM
 */
@Slf4j
@Component
public class JJTimer {

    @Value("${timer-flag}")
    private Boolean timerFlag;
    @Autowired
    private MairuiService mairuiService;
    @Autowired
    private HolidayUtil holidayUtil;

    //@Scheduled(cron = "0 0 23 * * ?")
    @DoTimer(dateType = 1)
    public void doJJGZ() {
        jjgz();
        jjgzcc();
    }

    private void jjgzcc() {
        ApiResponse apiResponse = mairuiService.jjHotCC(LocalDate.now());
        log.info("[持仓占比] {}", JSONObject.toJSONString(apiResponse));
    }

    public void jjgz() {
        try {
            List<JJGZ> jjgzs = mairuiService.jjHotGzAll();
            try {
                MailUtil.send("热门估值", MairuiUtil.getMessage(jjgzs));
            } catch (Exception e) {
                log.error(e.getMessage(),e);
                throw new RuntimeException(e);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            try {
                MailUtil.send("热门估值失败", ExceptionUtil.getExceptionToString(e));
            } catch (Exception ex) {
                log.error(ex.getMessage(),ex);
                throw new RuntimeException(ex);
            }
        }
    }


}
